<?php
/**
 * Created by PhpStorm.
 * User: 曹旺
 * Date: 2018-05-07
 * Time: 10:34
 */
class Curl{
    /**
     *封装get请求
     *params $url
     *author leesin.
     *date 2018/3/8
     */
    public function getContents($url){
        /*//初始化链接
        $ch = curl_init($url);
        //设置选项
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);
        //执行请求
        $response = curl_exec($ch);
        //关闭链接
        curl_close($ch);*/
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        //返回结果
        return $tmpInfo;
    }

    /**
     *拼接get请求
     *params url params
     *author leesin.
     *date 2018/3/8
     */
    public function get($baseUrl,$paramsArr){
        //拼接请求的url地址
        $url = $this->combineUrl($baseUrl,$paramsArr);
        //发送get请求
        return $this->getContents($url);
    }

    /**
     *拼接url地址
     *params url params
     *author leesin.
     *date 2018/3/8
     */
    public function combineUrl($baseUrl,$paramsArr){
        $url = $baseUrl;
        $json = '?';
        foreach ($paramsArr as $name=>$val){
            $url.=$json.$name.'='.$val;
            $json = '&';
        }
        return $url;
    }

    /**
     *发送post请求
     *params url params
     *author leesin.
     *date 2018/3/8
     */
    public function post($url,$paramsArr){
        //初始化链接
        $ch = curl_init($url);
        //设置选项
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$paramsArr);
        //执行请求
        $response = curl_exec($ch);
        //关闭链接
        curl_close($ch);
        //返回
        return $response;

    }
}